package com.sgb.kjwl.view.ui.activity.enterprise;

import android.view.View;
import android.widget.TextView;

import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPublishServiceListBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.view.ui.adapter.enterprise.PublishServiceAdapter;
import com.sgb.kjwl.viewmodel.enterprise.PublishServiceListActivityModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * Time:2021/4/16
 * Author:黄淋
 * Description:发布的服务列表
 */
public class PublishServiceListActivity extends AppActivity {
    ActivityPublishServiceListBinding mBinding;

    @Inject
    PublishServiceListActivityModel mModel;

    PublishServiceAdapter mPublishServiceAdapter;
    private Map<String, Object> mMap;
    private boolean mIsLoadMore;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder()
                .appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_publish_service_list);

        mBinding.actionBar.findViewById(R.id.right_view).setOnClickListener(this);
        mBinding.tvSearch.setOnClickListener(this);

        initAdapter();
    }

    @Override
    protected void init() {
        mMap = new HashMap<>();
        int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        mMap.put("publishType", String.valueOf(type));
        if (type == 2) {
            mMap.put("teamNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, ""));
            mMap.put("companyNo", "");
        } else if (type == 1) {
            mMap.put("teamNo", "");
            mMap.put("companyNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, ""));
        }
        mModel.ldServiceList.observe(this, list -> showQuoteList(list, mIsLoadMore));
        mModel.ldServiceStatus.observe(this, a -> mModel.getPublishServiceList(this, false, mMap));
    }

    @Override
    protected void onResume() {
        super.onResume();
        mIsLoadMore = false;
        mModel.getPublishServiceList(this, false, mMap);
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        mPublishServiceAdapter = new PublishServiceAdapter(this, null);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerView.setAdapter(mPublishServiceAdapter);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mIsLoadMore = false;
                mModel.getPublishServiceList(PublishServiceListActivity.this, false, mMap);
            }

            @Override
            public void onLoadMore() {
                mIsLoadMore = true;
                mModel.getPublishServiceList(PublishServiceListActivity.this, true, mMap);
            }
        });
        mPublishServiceAdapter.setOnItemClick(new AdapterOnItemClick<EpQuoteEntity>() {
            @Override
            public void onItemClick(EpQuoteEntity epQuoteEntity, int position) {
                ActivityJump.jumpToActivity(PublishServiceListActivity.this, PublishServiceDetailActivity.class, Pair.create("id", Long.parseLong(epQuoteEntity.getId())));
            }
        });
        mPublishServiceAdapter.OnCallPhone((id, status) -> {
            DialogHelper.showDialog(getActivity(), "温馨提示", status.equals("1") ? "是否确认下架？" : "是否确认上架？", "确定", new DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    mModel.getPublishServiceEnable(PublishServiceListActivity.this, id, status.equals("1") ? "-1" : "1");
                }
            });
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_view:
                if (!ClickUtils.isDoubleClick()) {
                    ActivityJump.jumpToActivity(PublishServiceListActivity.this, PublishServiceActivity.class);
                }
                break;
            case R.id.tv_search:
                if (!ClickUtils.isDoubleClick()) {
                    // TODO: 2021/4/16 搜索
                }
                break;
        }
    }


    /**
     * 发布的服务数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showQuoteList(List<EpQuoteEntity> list, boolean isLoadMore) {
        mBinding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                mBinding.recyclerView.loadMoreComplete();
                mPublishServiceAdapter.addDatas(list);
            } else {
                mBinding.recyclerView.setNoMore(true);
            }
            mBinding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            mBinding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
//                mBinding.llSearch.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                mBinding.emptyView.setVisibility(View.GONE);
                mPublishServiceAdapter.setDatas(list);
                mBinding.recyclerView.setLoadingMoreEnabled(true);
            } else {
//                mBinding.llSearch.setVisibility(View.GONE);
                mPublishServiceAdapter.setDatas(null);
                mBinding.recyclerView.setVisibility(View.GONE);
                mBinding.emptyView.setVisibility(View.VISIBLE);
                ((TextView) mBinding.include.findViewById(R.id.no_data_tv)).setText("无服务发布的记录哦");
            }
        }
    }
}
